using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Resources;
using System.Reflection;

using System.Xml.Serialization;

using FridayShopping.DataModel;

namespace FridayShopping.UI
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class MainWin : System.Windows.Forms.Form
	{
		private System.Windows.Forms.MainMenu mainMenu;
		private System.Windows.Forms.Button categoriesBtn;
		private System.Windows.Forms.Button shopsBtn;
		private System.Windows.Forms.Button productsBtn;
		private System.Windows.Forms.ContextMenu shopsMenu;
		private System.Windows.Forms.Button btn_goShopping;

		
		ResourceManager resources = new ResourceManager("FridayShopping.UI.Resources",
										System.Reflection.Assembly.GetExecutingAssembly());
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.Button statBtn;
		private System.Windows.Forms.Label lbl_Version;
		private System.Windows.Forms.MenuItem menuItem_exit;
		private System.Windows.Forms.MenuItem menuItem_Save;
		private System.Windows.Forms.MenuItem menuItem1;

		Hashtable menuItem2Shop;


		public MainWin()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			// Open the database
			try 
			{
				Database.Instance.Load();
			}
			catch (DatabaseException e) 
			{
				MessageBox.Show("Error Loading database " + e, "Friday Shopping");
			}

		}
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			base.Dispose( disposing );
		}

        //static void Main() 
        //{
        //    Application.Run(new MainWin());
        //}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainWin));
            this.mainMenu = new System.Windows.Forms.MainMenu();
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.menuItem_Save = new System.Windows.Forms.MenuItem();
            this.menuItem_exit = new System.Windows.Forms.MenuItem();
            this.categoriesBtn = new System.Windows.Forms.Button();
            this.shopsBtn = new System.Windows.Forms.Button();
            this.productsBtn = new System.Windows.Forms.Button();
            this.btn_goShopping = new System.Windows.Forms.Button();
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.statBtn = new System.Windows.Forms.Button();
            this.lbl_Version = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // mainMenu
            // 
            this.mainMenu.MenuItems.Add(this.menuItem1);
            // 
            // menuItem1
            // 
            this.menuItem1.MenuItems.Add(this.menuItem_Save);
            this.menuItem1.MenuItems.Add(this.menuItem_exit);
            resources.ApplyResources(this.menuItem1, "menuItem1");
            // 
            // menuItem_Save
            // 
            resources.ApplyResources(this.menuItem_Save, "menuItem_Save");
            this.menuItem_Save.Click += new System.EventHandler(this.menuItem_Save_Click);
            // 
            // menuItem_exit
            // 
            resources.ApplyResources(this.menuItem_exit, "menuItem_exit");
            this.menuItem_exit.Click += new System.EventHandler(this.menuItem_exit_Click);
            // 
            // categoriesBtn
            // 
            resources.ApplyResources(this.categoriesBtn, "categoriesBtn");
            this.categoriesBtn.Name = "categoriesBtn";
            this.categoriesBtn.Click += new System.EventHandler(this.categoriesBtn_Click);
            // 
            // shopsBtn
            // 
            resources.ApplyResources(this.shopsBtn, "shopsBtn");
            this.shopsBtn.Name = "shopsBtn";
            this.shopsBtn.Click += new System.EventHandler(this.shopsBtn_Click);
            // 
            // productsBtn
            // 
            resources.ApplyResources(this.productsBtn, "productsBtn");
            this.productsBtn.Name = "productsBtn";
            this.productsBtn.Click += new System.EventHandler(this.productsBtn_Click);
            // 
            // btn_goShopping
            // 
            resources.ApplyResources(this.btn_goShopping, "btn_goShopping");
            this.btn_goShopping.Name = "btn_goShopping";
            this.btn_goShopping.Click += new System.EventHandler(this.btn_goShopping_Click);
            // 
            // pictureBox1
            // 
            resources.ApplyResources(this.pictureBox1, "pictureBox1");
            this.pictureBox1.Name = "pictureBox1";
            // 
            // statBtn
            // 
            resources.ApplyResources(this.statBtn, "statBtn");
            this.statBtn.Name = "statBtn";
            this.statBtn.Click += new System.EventHandler(this.statBtn_Click);
            // 
            // lbl_Version
            // 
            resources.ApplyResources(this.lbl_Version, "lbl_Version");
            this.lbl_Version.Name = "lbl_Version";
            // 
            // MainWin
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
            resources.ApplyResources(this, "$this");
            this.Controls.Add(this.lbl_Version);
            this.Controls.Add(this.statBtn);
            this.Controls.Add(this.btn_goShopping);
            this.Controls.Add(this.productsBtn);
            this.Controls.Add(this.shopsBtn);
            this.Controls.Add(this.categoriesBtn);
            this.Controls.Add(this.pictureBox1);
            this.Menu = this.mainMenu;
            this.Name = "MainWin";
            this.Load += new System.EventHandler(this.MainWin_Load);
            this.Closing += new System.ComponentModel.CancelEventHandler(this.MainWin_Closing);
            this.ResumeLayout(false);

		}
		#endregion

		#region Form Event Handlers

		private void MainWin_Load(object sender, System.EventArgs e)
		{
			lbl_Version.Text = FridayShopping.Util.Global.Version;
		}

		private void categoriesBtn_Click(object sender, System.EventArgs e)
		{
			CategoriesForm form = new CategoriesForm();

			form.ShowDialog();
		}

		private void menuItem_exit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void menuItem_Save_Click(object sender, System.EventArgs e)
		{
			try 
			{
				Database.Instance.Save();
			}
			catch(DatabaseException)
			{
			}			
			MessageBox.Show(resources.GetString("databaseSaved"), resources.GetString("applName"));

		}

		private void shopsBtn_Click(object sender, System.EventArgs e)
		{
			ShopsForm form = new ShopsForm();

			form.ShowDialog();
		}

		private void productsBtn_Click(object sender, System.EventArgs e)
		{
			ProductsForm form = new ProductsForm();

			form.ShowDialog();
		}

		// Fill the popup menu with the list of all the shops
		private void btn_goShopping_Click(object sender, System.EventArgs e)
		{
			menuItem2Shop = new Hashtable();
			shopsMenu = new ContextMenu();
			Shops shops = Database.Instance.Shops;
			foreach (Shop shop in shops.List) 
			{
				MenuItem menuItem = new MenuItem();
				menuItem.Text = shop.Name;
				menuItem.Click+=new EventHandler(menuItem_Click);
				shopsMenu.MenuItems.Add(menuItem);
				menuItem2Shop[menuItem] = shop;
			}
			shopsMenu.Show(btn_goShopping, new Point(10,10));
		}

		private void menuItem_Click(object sender, EventArgs e)
		{
			Shop shop = (Shop)menuItem2Shop[sender];
			if (shop != null) 
			{
				ShopListForm shopForm = new ShopListForm(shop);
				shopForm.ShowDialog();
			}
		}

		private void MainWin_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			DialogResult result = MessageBox.Show(resources.GetString("areusure"), 
							resources.GetString("applName"),
							MessageBoxButtons.YesNoCancel,
							MessageBoxIcon.Question,
							MessageBoxDefaultButton.Button1);

			if (result == DialogResult.Cancel)
			{
				e.Cancel = true;				
			} 
			else if (result == DialogResult.Yes) 
			{
				Database.Instance.Save();
			}
		}

		private void statBtn_Click(object sender, System.EventArgs e)
		{
			try 
			{
				Statistics.Instance.Load(Database.Instance.ExportCSVFile);
			}
			catch (StatisticsException)
			{
				string text = LangResources.Instance.Resources.GetString("nostats");
				string caption = LangResources.Instance.Resources.GetString("applName");
				MessageBox.Show(text,caption);
				return;
			}

			StatisticsForm form = new StatisticsForm(Statistics.Instance);

			form.ShowDialog();
		}
		#endregion

	}
}

